﻿using MexicanControl.model;
using System;
using System.Linq;
using System.Windows;

namespace MexicanControl
{
    /// <summary>
    /// GoodsAdd.xaml 的交互逻辑
    /// </summary>
    public partial class GoodsAdd : Window
    {
        public GoodsAdd()
        {
            InitializeComponent();
        }
        public GoodsMsg Goods { get; set; }
        public bool UpdBol { get; set; } = false;
        private void Canl_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!UpdBol)
                {
                    GoodsMsg msg = new GoodsMsg();
                    msg.sp_Name = SP_Name.Text;
                    msg.sp_Code = SP_Code.Text;
                    msg.CoffeeWZ = Convert.ToInt32(CoffeeWZ.Text);
                    msg.CoffeeWZ1 = Convert.ToInt32(CoffeeWZ1.Text);
                    msg.CoffeeWZ2 = Convert.ToInt32(CoffeeWZ2.Text);
                    msg.Sugar = Sugar1.IsChecked == true ? "1" : "0";
                    MainWindow.goodsMsgs.Add(msg);
                    this.DialogResult = true;
                    this.Close();
                }
                else
                {
                    var dd = MainWindow.goodsMsgs.FirstOrDefault(a => a.sp_Code == Goods.sp_Code && a.Sugar == Goods.Sugar && a.sp_Name == Goods.sp_Name && a.CoffeeWZ == Goods.CoffeeWZ && a.CoffeeWZ1 == Goods.CoffeeWZ1 && a.CoffeeWZ2 == Goods.CoffeeWZ2);
                    if (dd != null)
                    {
                        dd.Sugar = Sugar1.IsChecked == true ? "1" : "0";
                        dd.sp_Name = SP_Name.Text;
                        dd.sp_Code = SP_Code.Text;
                        dd.CoffeeWZ = Convert.ToInt32(CoffeeWZ.Text);
                        dd.CoffeeWZ1 = Convert.ToInt32(CoffeeWZ1.Text);
                        dd.CoffeeWZ2 = Convert.ToInt32(CoffeeWZ2.Text);
                    }
                    this.DialogResult = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (UpdBol)
                {
                    if (Goods != null)
                    {
                        SP_Name.Text = Goods.sp_Name;
                        SP_Code.Text = Goods.sp_Code;
                        CoffeeWZ.Text = Goods.CoffeeWZ.ToString();
                        CoffeeWZ1.Text = Goods.CoffeeWZ1.ToString();
                        CoffeeWZ2.Text = Goods.CoffeeWZ2.ToString();
                        NoSugar.IsChecked = Goods.Sugar == "0";
                        Sugar1.IsChecked = Goods.Sugar == "1";
                    }
                }
            }
            catch
            {
            }
        }
    }
}
